<template>
  <div style="height: 100vh; display: flex; align-items: center; justify-content: center; background-color: #0f9876">
    <div style="display: flex; background-color: white; width: 50%; border-radius: 5px; overflow: hidden">
      <div style="flex: 1">
        <img src="@/assets/login.png" alt="" style="width: 100%">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center">
        <el-form :model="user" style="width: 80%" :rules="rules" ref="loginRef">
          <div style="font-size: 20px; font-weight: bold; text-align: center;; margin-bottom: 30px">欢迎登录后台管理系统</div>
          <el-form-item prop="username" >
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号" v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码" v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="login">登 录</el-button>
          </el-form-item>
          <div style="display: flex">
            <div style="flex: 1">还没有账号？请 <span style="color: #0f9876; cursor: pointer" @click="$router.push('/register')">注册</span></div>
              <div style="flex: 1; text-align: right"><span style="color: #0f9876; cursor: pointer" @click="handleForgetPass">忘记密码</span></div>
          </div>


          <el-dialog title="忘记密码" :visible.sync="forgetPasswordDialogVis" width="30%" >
            <el-form :model="forgetUserFrom" label-width="80px" style="padding-right: 20px">
              <el-form-item label="用户名"  style="font-size: 20px">
                <el-input v-model="forgetUserFrom.username" autocomplete="off" placeholder="请输入用户名"></el-input>
              </el-form-item>
              <el-form-item label="手机号" >
                <el-input v-model="forgetUserFrom.phone" autocomplete="off" placeholder="请输入手机号"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="forgetPasswordDialogVis = false">取 消</el-button>
              <el-button type="primary" @click="resetPassword">确 定</el-button>
            </div>
          </el-dialog>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// 删除验证码组件的导入
export default {
  name: "Login",
  data() {
    // 删除验证码相关的数据属性和验证规则
    return {
      forgetUserFrom:{},  //忘记密码表单数据
      forgetPasswordDialogVis: false,
      user: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
      }
    }
  },
  methods: {
    handleForgetPass(){
      this.forgetUserFrom = {}
      this.forgetPasswordDialogVis = true

    },//初始化表单
    resetPassword() {

      this.$request.put('/password', this.forgetUserFrom).then(res => {
        if (res.code === '200') {
          this.$message.success('重置密码成功')
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    login() {
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
          // 验证通过
          this.$request.post('/login', this.user).then(res => {
            if (res.code === '200') {
              this.$router.push('/')
              this.$message.success('登录成功')
              localStorage.setItem("honey-user", JSON.stringify(res.data))  // 存储用户数据
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
